package com.gehj.uhttp.CallBack;

import android.os.Handler;
import android.os.Looper;

import com.gehj.uhttp.listener.DisposeDataListener;


import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 *
 * 因为在OK的CallBack回调中需要一个一个处理，利用这个类，对请求进行集中处理
 * 比如改toast-->log日志，401的统一处理等
 * @author vision
 * @function 专门处理JSON的回调
 */
public class MyJsonCallback implements Callback {


    /**
     * the java layer exception, do not same to the logic error
     */
    protected final int NETWORK_ERROR = -1; // the network relative error
    protected final int JSON_ERROR = -2; // the JSON relative error
    protected final int OTHER_ERROR = -3; // the unknow error


    private Handler mDeliveryHandler;
    private DisposeDataListener mListener;
    private Class<?> mClass;

    public MyJsonCallback(DisposeDataListener listener) {
        this.mListener = listener;
        this.mDeliveryHandler = new Handler(Looper.getMainLooper());
    }

    @Override
    public void onFailure(final Call call, final IOException ioexception) {
        /**
         * 可以在UI线程吐司
         */
        /*mDeliveryHandler.post(new Runnable() {
            @Override
            public void run() {
                mListener.onFailure(ioexception.getMessage());
            }
        });
        LogUtil.e(getClass().getName(),ioexception.getMessage());*/
        //可以在子线程直接打log日志
        mListener.onFailure(ioexception.getMessage());
    }


    @Override
    public void onResponse(final Call call, final Response response) throws IOException {
        /*mDeliveryHandler.post(new Runnable() { //UI线程处理
            @Override
            public void run() {
                mListener.onSuccess(response);
            }
        });*/

             mListener.onSuccess(response);//子线程处理
    }


}